﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml;
using H2.Streams;

namespace Mystery
{
    public class XMLInfo
    {
        public FileInfo FileInfo;

        public string TagName { get; set; }
        public string TagClass { get; set; }
        public string Offset { get { return "0"; } }
        public int Size { get; set; }
        public string Magic { get { return "0"; } }
        public string Parsed { get { return "True"; } }
        public string Date { get { return DateTime.Now.ToShortDateString(); } }
        public string Time { get { return DateTime.Now.ToShortTimeString(); } }
        public string EntityVersion { get { return "0.1"; } }
        public string Padding { get { return "0"; } }

        XmlTextWriter xw;

        public XMLInfo(string filename)
        {
            xw = new XmlTextWriter(new FileStream(filename, FileMode.Create), Encoding.Default);
            xw.Formatting = Formatting.Indented;

            FileInfo = new FileInfo(filename);
        }

        public void WriteHeader()
        {
            xw.WriteStartElement("Meta");
            xw.WriteAttributeString("TagType", TagClass);
            xw.WriteAttributeString("TagName", TagName);
            xw.WriteAttributeString("Offset", Offset);
            xw.WriteAttributeString("Size", Size.ToString());
            xw.WriteAttributeString("Magic", Magic);
            xw.WriteAttributeString("Parsed", Parsed);
            xw.WriteAttributeString("Date", Date);
            xw.WriteAttributeString("Time", Time);
            xw.WriteAttributeString("EntityVersion", EntityVersion);
            xw.WriteAttributeString("Padding", Padding);
        }

        public void WriteReflexive(int offset, int chunkCount, int chunkSize, int startOffset)
        {
            xw.WriteStartElement("Reflexive");

            xw.WriteAttributeString("Description", "Reflexive");
            xw.WriteAttributeString("Offset", offset.ToString());

            xw.WriteAttributeString("ChunkCount", chunkCount.ToString());
            xw.WriteAttributeString("ChunkSize", chunkSize.ToString());
            xw.WriteAttributeString("Translation", startOffset.ToString());
            xw.WriteAttributeString("PointsToTagType", TagClass);
            xw.WriteAttributeString("PointsToTagName", TagName);

            xw.WriteAttributeString("TagType", TagClass);
            xw.WriteAttributeString("TagName", TagName);
            xw.WriteEndElement();
        }

        public void WriteIdent(int offset, string tagClass, string tagPath)
        {
            xw.WriteStartElement("Ident");

            xw.WriteAttributeString("Description", "Ident");
            xw.WriteAttributeString("Offset", offset.ToString());

            xw.WriteAttributeString("PointsToTagType", tagClass);
            xw.WriteAttributeString("PointsToTagName", tagPath);

            xw.WriteAttributeString("TagType", TagClass);
            xw.WriteAttributeString("TagName", TagName);
            xw.WriteEndElement();
        }

        public void WriteStringID(int offset, string name)
        {
            xw.WriteStartElement("String");

            xw.WriteAttributeString("Description", "String");
            xw.WriteAttributeString("Offset", offset.ToString());

            xw.WriteAttributeString("StringName", name);

            xw.WriteAttributeString("TagType", TagClass);
            xw.WriteAttributeString("TagName", TagName);
            xw.WriteEndElement();
        }

        public void WriteEnd()
        { xw.WriteEndElement(); }

        public void FlushAndClose()
        {
            xw.Flush();
            xw.Close();
        }
    }
}
